import numpy as np
import matplotlib.pyplot as plt
# 阶跃函数
def step_function(x):
    '''当输入超过0时，输出1，否则输出0.
        params
        -------------
        x: 输入参数（numpy 数组）
        return 
        ------------
        与输入数组等长的numpy数组，元素为0 or 1
    '''
    y = x>0
    return y.astype(np.int32)

def sigmoid(x):
    return 1/(1+np.exp(-x))


def relu(x):
    return np.maximum(0,x) 

def identity_function(x):
    return x 

def softmax(x):
    if x.ndim == 2:
        x = x.T
        x = x-np.max(x,axis=0)
        y = np.exp(x)/np.sum(np.exp(x),axis=0)
        return y.T
    x=x-np.max(x)
    return np.exp(x)/np.sum(np.exp(x))

# 均方误差函数
def mean_squared_error(y,t):
    return 0.5*np.sum((y-t)**2)

# 交叉熵函数
def cross_entropy_error(y,t):
    if y.ndim ==1:
        y = y.reshape(1,y.size)
        t = y.reshape(1,t.size)
    # 监督数据是one_hot_label的情况下，转换正确的标签下标
    if t.size == y.size:
        t = t.argmax(axis=1)
    
    batch_size = y.shape[0]
    return -np.sum(np.log(y[np.arange(batch_size),t]+1e-7))/batch_size

if __name__ == '__main__':
    t = [0, 0, 1, 0, 0, 0, 0, 0, 0, 0]
    y = [0.1, 0.05, 0.6, 0.0, 0.05, 0.1, 0.0, 0.1, 0.0, 0.0]
    print(cross_entropy_error(np.array(y),np.array(t)))
